#include "qcelleditdialog.h"

void QCellEditDialog::createButtons(int row)
{
    //Buttons creation. They are connected by slotsByName
    acceptButton = new QPushButton(tr("Accept"));
    acceptButton->setObjectName("acceptButton");
    mainLayout->addWidget(acceptButton, row, 0, 1, 1);
    deleteButton = new QPushButton(tr("Delete"));
    deleteButton->setObjectName("deleteButton");
    mainLayout->addWidget(deleteButton, row, 1, 1, 1);
    closeButton = new QPushButton(tr("Close"));
    closeButton->setObjectName("closeButton");
    mainLayout->addWidget(closeButton, row, 2, 1, 1);
}

void QCellEditDialog::addCell(void)
{
    QTreeWidgetItem *cellPropertyItem;

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "beta");
    cellPropertyItem->setText(1, QString::number(cell.beta));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "dt");
    cellPropertyItem->setText(1, QString::number(cell.dt));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "eMax");
    cellPropertyItem->setText(1, QString::number(cell.eMax));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "eMin");
    cellPropertyItem->setText(1, QString::number(cell.eMin));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "fpol");
    cellPropertyItem->setText(1, QString::number(cell.fpol));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "fadh");
    cellPropertyItem->setText(1, QString::number(cell.fadh));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "fcon");
    cellPropertyItem->setText(1, QString::number(cell.fcon));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "h");
    cellPropertyItem->setText(1, QString::number(cell.h));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "scale");
    cellPropertyItem->setText(1, QString::number(cell.scale));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "sigma");
    cellPropertyItem->setText(1, QString::number(cell.sigma));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "sigmaAV");
    cellPropertyItem->setText(1, QString::number(cell.sigmaAV));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);

    cellPropertyItem = new QTreeWidgetItem(treeWidget->topLevelItem(0));
    cellPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
    cellPropertyItem->setText(0, "t");
    cellPropertyItem->setText(1, QString::number(cell.t));
    treeWidget->topLevelItem(0)->addChild(cellPropertyItem);
}

void QCellEditDialog::addNodes(void)
{
    for (int i=0; i<cell.nodes.length(); ++i)
    {
        QTreeWidgetItem *nodeItem = new QTreeWidgetItem(treeWidget->topLevelItem(1));
        nodeItem->setText(0, "node #" + QString::number(i));

        QTreeWidgetItem *nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        nodePropertyItem->setText(0, "isBoundary");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].isBoundary));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        nodePropertyItem->setText(0, "isFrontal");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].isFrontal));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        nodePropertyItem->setText(0, "pos.x");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].pos.x()));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        nodePropertyItem->setText(0, "pos.y");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].pos.y()));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        nodePropertyItem->setText(0, "fx");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].fx));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        nodePropertyItem->setText(0, "fy");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].fy));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        nodePropertyItem->setText(0, "sigma");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].sigma));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        nodePropertyItem->setText(0, "adhesionPos.x");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].adhesionPos.x()));
        nodeItem->addChild(nodePropertyItem);

        nodePropertyItem = new QTreeWidgetItem(nodeItem);
        nodePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        nodePropertyItem->setText(0, "adhesionPos.y");
        nodePropertyItem->setText(1, QString::number(cell.nodes[i].adhesionPos.y()));
        nodeItem->addChild(nodePropertyItem);
    }
}

void QCellEditDialog::addEdges(void)
{
    for (int i=0; i<cell.edges.length(); ++i)
    {
        QTreeWidgetItem *edgeItem = new QTreeWidgetItem(treeWidget->topLevelItem(2));
        edgeItem->setText(0, "edge #" + QString::number(i));

        QTreeWidgetItem *edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "defaultLength");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].defaultLength));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "id1");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].id1));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "id2");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].id2));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "id3");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].id3));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "id4");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].id4));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "isBoundary");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].isBoundary));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "previousLength");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].previousLength));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "sigma");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].sigma));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "fcontract");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].fContract));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "area");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].area));
        edgeItem->addChild(edgePropertyItem);

        edgePropertyItem = new QTreeWidgetItem(edgeItem);
        edgePropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        edgePropertyItem->setText(0, "kappa");
        edgePropertyItem->setText(1, QString::number(cell.edges[i].kappa));
        edgeItem->addChild(edgePropertyItem);
    }
}

void QCellEditDialog::addAreas(void)
{
    for (int i=0; i<cell.areas.length(); ++i)
    {
        QTreeWidgetItem *areaItem = new QTreeWidgetItem(treeWidget->topLevelItem(3));
        areaItem->setText(0, "area #" + QString::number(i));

        QTreeWidgetItem *areaPropertyItem = new QTreeWidgetItem(areaItem);
        areaPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        areaPropertyItem->setText(0, "topLeft.x");
        areaPropertyItem->setText(1, QString::number(cell.areas[i].rect.topLeft().x()));
        areaItem->addChild(areaPropertyItem);

        areaPropertyItem = new QTreeWidgetItem(areaItem);
        areaPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        areaPropertyItem->setText(0, "topLeft.y");
        areaPropertyItem->setText(1, QString::number(cell.areas[i].rect.topLeft().y()));
        areaItem->addChild(areaPropertyItem);

        areaPropertyItem = new QTreeWidgetItem(areaItem);
        areaPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        areaPropertyItem->setText(0, "width");
        areaPropertyItem->setText(1, QString::number(cell.areas[i].rect.width()));
        areaItem->addChild(areaPropertyItem);

        areaPropertyItem = new QTreeWidgetItem(areaItem);
        areaPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        areaPropertyItem->setText(0, "height");
        areaPropertyItem->setText(1, QString::number(cell.areas[i].rect.height()));
        areaItem->addChild(areaPropertyItem);

        areaPropertyItem = new QTreeWidgetItem(areaItem);
        areaPropertyItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable);
        areaPropertyItem->setText(0, "sigma");
        areaPropertyItem->setText(1, QString::number(cell.areas[i].sigma));
        areaItem->addChild(areaPropertyItem);
    }
}

QCellEditDialog::QCellEditDialog(QMainCore *_core, QWidget *parent) : QDialog(parent), core(_core)
{
    this->setModal(true);
    this->setWindowFlags(Qt::Tool);
    this->resize(200, 600);
    mainLayout = new QGridLayout(this);
    cell = core->getCell();

    QStringList labels;
    labels << tr("Property") << tr("Value");

    treeWidget = new QTreeWidget(this);
    treeWidget->header()->setResizeMode(QHeaderView::Stretch);
    treeWidget->setHeaderLabels(labels);
    mainLayout->addWidget(treeWidget,0,0,1,3);

    QTreeWidgetItem *cellFolder = new QTreeWidgetItem(treeWidget);
    cellFolder ->setText(0,tr("cell"));
    treeWidget->addTopLevelItem(cellFolder);
    this->addCell();

    QTreeWidgetItem *nodesFolder = new QTreeWidgetItem(treeWidget);
    nodesFolder ->setText(0,tr("nodes"));
    treeWidget->addTopLevelItem(nodesFolder);
    if (cell.nodes.length()) this->addNodes();

    QTreeWidgetItem *edgesFolder = new QTreeWidgetItem(treeWidget);
    edgesFolder ->setText(0,tr("edges"));
    treeWidget->addTopLevelItem(edgesFolder);
    if (cell.edges.length()) this->addEdges();

    QTreeWidgetItem *areasFolder = new QTreeWidgetItem(treeWidget);
    areasFolder ->setText(0,tr("areas"));
    treeWidget->addTopLevelItem(areasFolder);
    if (cell.areas.length()) this->addAreas();

    this->createButtons(1);

    QMetaObject::connectSlotsByName(this);
    this->setLayout(mainLayout);
}

void QCellEditDialog::on_acceptButton_clicked(void)
{
    cell.nodes.clear();
    cell.edges.clear();
    cell.areas.clear();

    //Cell
    for (int j=0; j<treeWidget->topLevelItem(0)->childCount(); ++j)
    {
        if (treeWidget->topLevelItem(0)->child(j)->text(0) == "beta")
            cell.beta = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "dt")
            cell.dt = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "eMax")
            cell.eMax = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "eMin")
            cell.eMin = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "fpol")
            cell.fpol = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "fadh")
            cell.fadh = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "fcon")
            cell.fcon = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "h")
            cell.h = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "scale")
            cell.scale = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "sigmaAV")
            cell.sigmaAV = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "sigma")
            cell.sigma = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
        else if (treeWidget->topLevelItem(0)->child(j)->text(0) == "t")
            cell.t = treeWidget->topLevelItem(0)->child(j)->text(1).toDouble();
    }

    //Nodes
    for (int i=0; i<treeWidget->topLevelItem(1)->childCount(); ++i)
    {
        NODE node;

        for (int j=0; j<treeWidget->topLevelItem(1)->child(i)->childCount(); ++j)
        {
            if (treeWidget->topLevelItem(1)->child(i)->child(j)->text(0) == "isBoundary")
                node.isBoundary = treeWidget->topLevelItem(1)->child(i)->child(j)->text(1).toInt();
            else if (treeWidget->topLevelItem(1)->child(i)->child(j)->text(0) == "isFrontal")
                node.isFrontal = treeWidget->topLevelItem(1)->child(i)->child(j)->text(1).toInt();
            else if (treeWidget->topLevelItem(1)->child(i)->child(j)->text(0) == "pos.x")
                node.pos = QPointF(treeWidget->topLevelItem(1)->child(i)->child(j)->text(1).toDouble(), node.pos.y());
            else if (treeWidget->topLevelItem(1)->child(i)->child(j)->text(0) == "pos.y")
                node.pos = QPointF(node.pos.x(), treeWidget->topLevelItem(1)->child(i)->child(j)->text(1).toDouble());       
            else if (treeWidget->topLevelItem(1)->child(i)->child(j)->text(0) == "adhesionPos.x")
                node.adhesionPos = QPointF(treeWidget->topLevelItem(1)->child(i)->child(j)->text(1).toDouble(), node.adhesionPos.y());
            else if (treeWidget->topLevelItem(1)->child(i)->child(j)->text(0) == "adhesionPos.y")
                node.adhesionPos = QPointF(node.adhesionPos.x(), treeWidget->topLevelItem(1)->child(i)->child(j)->text(1).toDouble());
        }

        cell.nodes << node;
    }

    //Edges
    for (int i=0; i<treeWidget->topLevelItem(2)->childCount(); ++i)
    {
        EDGE edge;

        for (int j=0; j<treeWidget->topLevelItem(2)->child(i)->childCount(); ++j)
        {
            if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "area")
                edge.area = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toDouble();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "defaultLength")
                edge.defaultLength = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toDouble();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "id1")
                edge.id1 = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toInt();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "id2")
                edge.id2 = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toInt();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "id3")
                edge.id3 = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toInt();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "id4")
                edge.id4 = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toInt();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "isBoundary")
                edge.isBoundary = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toInt();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "kappa")
                edge.kappa = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toDouble();
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "pos.x")
                edge.pos = QPointF(treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toDouble(), edge.pos.y());
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "pos.y")
                edge.pos = QPointF(edge.pos.x(), treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toDouble());
            else if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0) == "previousLength")
                edge.previousLength = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toDouble();
        }

        cell.edges << edge;
    }

    //Areas
    for (int i=0; i<treeWidget->topLevelItem(3)->childCount(); ++i)
    {
        AREA area;

        for (int j=0; j<treeWidget->topLevelItem(3)->child(i)->childCount(); ++j)
        {
            if (treeWidget->topLevelItem(3)->child(i)->child(j)->text(0) == "topLeft.x")
                area.rect = QRectF(treeWidget->topLevelItem(3)->child(i)->child(j)->text(1).toDouble(),
                                   area.rect.topLeft().y(),
                                   area.rect.width(),
                                   area.rect.height());
            else if (treeWidget->topLevelItem(3)->child(i)->child(j)->text(0) == "topLeft.y")
                area.rect = QRectF(area.rect.topLeft().x(),
                                   treeWidget->topLevelItem(3)->child(i)->child(j)->text(1).toDouble(),
                                   area.rect.width(),
                                   area.rect.height());
            else if (treeWidget->topLevelItem(3)->child(i)->child(j)->text(0) == "width")
                area.rect = QRectF(area.rect.topLeft().x(),
                                   area.rect.topLeft().y(),
                                   treeWidget->topLevelItem(3)->child(i)->child(j)->text(1).toDouble(),
                                   area.rect.height());
            else if (treeWidget->topLevelItem(3)->child(i)->child(j)->text(0) == "height")
                area.rect = QRectF(area.rect.topLeft().x(),
                                   area.rect.topLeft().y(),
                                   area.rect.width(),
                                   treeWidget->topLevelItem(3)->child(i)->child(j)->text(1).toDouble());
            else if (treeWidget->topLevelItem(3)->child(i)->child(j)->text(0) == "sigma")
                area.sigma = treeWidget->topLevelItem(3)->child(i)->child(j)->text(1).toDouble();
        }

        cell.areas << area;
    }

    core->setCell(cell);
    this->close();
}

void QCellEditDialog::on_deleteButton_clicked(void)
{
    int id = treeWidget->currentItem()->text(0).section('#',1,1).toInt();

    if (treeWidget->currentItem()->text(0).contains("node"))
    {
        treeWidget->topLevelItem(1)->removeChild(treeWidget->currentItem());

        for (int i=0; i<treeWidget->topLevelItem(1)->childCount(); ++i)
        {
            int a = treeWidget->topLevelItem(1)->child(i)->text(0).section('#',1,1).toInt();

            if (a>id)
            {
                treeWidget->topLevelItem(1)->child(i)->setText(0, "node #" + QString::number(a-1));
            }
        }

        for (int i=treeWidget->topLevelItem(2)->childCount()-1; i>=0; --i)
        {
            bool toBeDeleted = false;

            for (int j=0; j<treeWidget->topLevelItem(2)->child(i)->childCount(); ++j)
            {
                if (treeWidget->topLevelItem(2)->child(i)->child(j)->text(0).contains("id"))
                {
                    int a = treeWidget->topLevelItem(2)->child(i)->child(j)->text(1).toInt();

                    if (a == id)
                        toBeDeleted = true;
                    else if (a > id)
                        treeWidget->topLevelItem(2)->child(i)->child(j)->setText(1, QString::number(a-1));
                }
            }

            if (toBeDeleted) treeWidget->topLevelItem(2)->removeChild(treeWidget->topLevelItem(2)->child(i));
            toBeDeleted = false;
        }
    }
    else if (treeWidget->currentItem()->text(0).contains("edge"))
    {
        treeWidget->topLevelItem(2)->removeChild(treeWidget->currentItem());

        for (int i=0; i<treeWidget->topLevelItem(2)->childCount(); ++i)
        {
            int a = treeWidget->topLevelItem(2)->child(i)->text(0).section('#',1,1).toInt();

            if (a>id)
            {
                treeWidget->topLevelItem(2)->child(i)->setText(0, "edge #" + QString::number(a-1));
            }
        }
    }
    else if (treeWidget->currentItem()->text(0).contains("area"))
    {
        treeWidget->topLevelItem(3)->removeChild(treeWidget->currentItem());

        for (int i=0; i<treeWidget->topLevelItem(3)->childCount(); ++i)
        {
            int a = treeWidget->topLevelItem(3)->child(i)->text(0).section('#',1,1).toInt();

            if (a>id)
                treeWidget->topLevelItem(3)->child(i)->setText(0, "area #" + QString::number(a-1));
        }
    }
}

void QCellEditDialog::on_closeButton_clicked(void)
{
    this->close();
}
